﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using System.Data;
using BOHVote.BLL;
using BOHVote.Entity;

namespace BOH.Exam
{
    public partial class TestResultSearch : System.Web.UI.Page
    {
        BHUser user = null;
        private string UserID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                if (Session["User"] != null)
                {
                    user = (BHUser)Session["User"];
                    if (user.RightType == "C" || user.RightType == "A")
                    {
                        LoadDrop();
                        UnitGovernBind("");
                        ApplyTestResule(0);
                    }
                    UserID = user.UserID;

                }
                else
                {

                }
                
            }
        }

        #region <<  绑定查询条件选项  >>

        public void LoadDrop()
        {
            
           // BindCurrentJobRank();
            BindApplyType();
            //BindApplyStyle();

           // SubjectBind(drpApplyStyle.SelectedValue);
            //BindPeriod();
        }

        public void BindApplyType()
        {
            DataSet dst = OtherClass.dstgetApplyType();
           // BindDropDownList(drpApplyType, dst);
        }

        public void BindDropDownList(ListControl Controls, DataSet dst)
        {
            Controls.Items.Clear();
            ListItem list = new ListItem("请选择", "-1");
            Controls.Items.Add(list);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem itemlist = new ListItem(dr["dict_Display"].ToString(), dr["dict_Value"].ToString());
                Controls.Items.Add(itemlist);
            }
        }

        //public void BindCurrentJobRank()
        //{
        //    drpApplyJobRank.Items.Clear();
        //    ListItem li1 = new ListItem("请选择学科组", "-1");
        //    drpApplyJobRank.Items.Add(li1);
        //    DataSet dst = OtherClass.getJobRank();
        //    foreach (DataRow dr in dst.Tables[0].Rows)
        //    {
        //        ListItem li = new ListItem(dr["JobRankName"].ToString(), dr["JobRankID"].ToString() + "," + dr["JobRankLevel"].ToString());
        //        drpApplyJobRank.Items.Add(li);
        //    }
        //}

        #region <<  绑定申请学科组和专业  >>


        //public void SubjectBind(string level)
        //{
        //    DataSet dst ;
        //    if(!string.IsNullOrEmpty(level))
        //        dst = Subject.getSubjectByLevel(level);
        //    else
        //        dst = Subject.getAllSubject();
        //    this.drpSubject.Items.Clear();
        //    ListItem li1 = new ListItem("请选择学科组", "-1");
        //    this.drpSubject.Items.Add(li1);
        //    foreach (DataRow dr in dst.Tables[0].Rows)
        //    {
        //        if (dr["SubjectID"].ToString().Length > 0)
        //        {
        //            ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
        //            this.drpSubject.Items.Add(li);

        //        }
        //    }
        //}

        //protected void drpSubject_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    string strSubjectID = ((DropDownList)sender).SelectedValue;
        //    if (strSubjectID != "-1")
        //    {
        //        SpecialityBind(strSubjectID);
        //    }
        //    else
        //    {
        //        drpSpeciality.Items.Clear();
        //        ListItem li1 = new ListItem("请选择专业", "-1");
        //        this.drpSpeciality.Items.Add(li1);
        //    }
        //}

        //public void SpecialityBind(string SubjectID)
        //{
        //    drpSpeciality.Items.Clear();
        //    ListItem li1 = new ListItem("请选择专业", "-1");
        //    this.drpSpeciality.Items.Add(li1);
        //    DataSet dst = Subject.getAllSpecialityBySubjectID(SubjectID);
        //    foreach (DataRow dr in dst.Tables[0].Rows)
        //    {
        //        ListItem li = new ListItem(dr["SpecialityName"].ToString(), dr["SpecialityID"].ToString());
        //        this.drpSpeciality.Items.Add(li);
        //    }
        //}

        #endregion

        #region <<  主管局和工作单位绑定  >>

        public void UnitGovernBind(string GovernID)
        {
            DataSet dstGovern = null;
            ListItem list = new ListItem("请选择主管局", "-1");
            drpGovern.Items.Add(list);
            dstGovern = UnitClass.getGovernByGovernID(GovernID);

            if (dstGovern.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in dstGovern.Tables[0].Rows)
                {
                    if (row["GovernID"].ToString().Trim().Length != 0)
                    {
                        ListItem li = new ListItem(row["GovernName"].ToString(), row["GovernID"].ToString());
                        this.drpGovern.Items.Add(li);
                        //this.bindDropChildItem(row["GovernID"].ToString());
                    }
                }
            }
        }

        protected void drpGovern_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strGovernSelected = ((DropDownList)sender).SelectedValue;
            if (strGovernSelected != "-1")
            {
                string GovernID = strGovernSelected;
                bindDropChildItem(GovernID);
            }
            else
            {
                drpUnit.Items.Clear();
                ListItem list = new ListItem("请选择工作单位", "-1");
                drpUnit.Items.Add(list);
            }
        }

        //protected void drpApplyStyle_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    string level = drpApplyStyle.SelectedValue;
        //    SubjectBind(level);
        //    drpSpeciality.Items.Clear();
        //    ListItem li1 = new ListItem("请选择专业", "-1");
        //    this.drpSpeciality.Items.Add(li1);
        //}

        public void bindDropChildItem(string GovernID)
        {
            drpUnit.Items.Clear();
            ListItem list = new ListItem("请选择工作单位", "-1");
            drpUnit.Items.Add(list);
            DataSet dstUnit = UnitClass.getUnitByGovern(GovernID);
            foreach (DataRow dr in dstUnit.Tables[0].Rows)
            {
                drpUnit.Items.Add(new ListItem(dr["UnitName"].ToString(), dr["UnitID"].ToString()));
            }
        }

        //public void UnitDropBind(string UnitID, string GovernID)
        //{
        //    string strUnitName = UnitClass.getUnitName(UnitID);
        //    ListItem li = new ListItem(strUnitName, UnitID);
        //    drpUnit.Items.Add(li);
        //    string strGovernName = UnitClass.GetGovernName(GovernID);
        //    ListItem liGovern = new ListItem(strUnitName, UnitID);
        //    drpGovern.Items.Add(liGovern);
        //}

        #endregion



        #endregion

        #region <<  绑定GridView  >>
        public void ApplyTestResule(int flag)
        {
            DataSet dst = ApplyTestClass.getAllTestResult((string)Session["ApplyPeriod"], txtIDCard.Text.Trim(), txtApplyName.Text.Trim(), drpGovern.SelectedValue, drpUnit.SelectedValue);
            //foreach(
            bindTestResule(dst, flag);
        }

        public void buildGridView(DataSet dst,int flag)
        {
            ApplyTestList.Columns.Clear();

            BoundField bf1 = new BoundField();
            bf1.HeaderText = "姓  名";
            bf1.ReadOnly = true;
            bf1.DataField = "Apply_Name";
            ApplyTestList.Columns.Add(bf1);

            BoundField bfG = new BoundField();
            bfG.HeaderText = "主管局";
            bfG.ReadOnly = true;
            bfG.DataField = "GovernID";
            ApplyTestList.Columns.Add(bfG);

            BoundField bf2 = new BoundField();
            bf2.HeaderText = "工作单位";
            bf2.ReadOnly = true;
            bf2.DataField = "UnitID";
            ApplyTestList.Columns.Add(bf2);

            BoundField bf3 = new BoundField();
            bf3.HeaderText = "身份证";
            bf3.ReadOnly = true;
            bf3.DataField = "IDCard";
            ApplyTestList.Columns.Add(bf3);

            int i = 8;
            while (i < dst.Tables[0].Columns.Count)
            {
                TemplateField tf = new TemplateField();

                tf.HeaderTemplate = new UserTemplate(dst.Tables[0].Columns[i+1].Caption);
                if(flag==0)
                    //tf.ItemTemplate = new UserTemplate(DataControlRowType.DataRow, "", i + 1, 0);
                    tf.ItemTemplate = new UserTemplate(DataControlRowType.DataRow, "Htextbox", i + 1, i);
                else
                    tf.EditItemTemplate = new UserTemplate(DataControlRowType.DataRow, "Htextbox", i + 1, i);
                ApplyTestList.Columns.Add(tf);

                i = i + 2;
            }
        }

        public void bindTestResule(DataSet dst, int flag)
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                buildGridView(dst, flag);
                AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
                if (AspNetPager1.RecordCount != 0)
                {
                    labNoRecord.Visible = false;
                    AspNetPager1.Visible = true;
                    PagedDataSource pds = new PagedDataSource();
                    pds.DataSource = dst.Tables[0].DefaultView;
                    pds.AllowPaging = true;
                    pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                    pds.PageSize = AspNetPager1.PageSize;
                    ApplyTestList.DataSource = pds;
                    ApplyTestList.DataBind();
                }
                else
                {
                    ApplyTestList.DataSource = null;
                    ApplyTestList.DataBind();
                    AspNetPager1.Visible = false;
                    labNoRecord.Visible = true;
                }
            }
            else
            {
                AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
                ApplyTestList.DataSource = null;
                ApplyTestList.DataBind();
            }
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            ApplyTestResule(0);
        }

        #endregion

        protected void ApplyTestList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ApplyTestList.EditIndex = e.NewEditIndex;
            ApplyTestResule(1);
        }

        protected void ApplyTestList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            ApplyTestList.EditIndex = -1;
            ApplyTestResule(0);
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            ApplyTestResule(0);
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (hiddenCourseID.Value.Trim().Length > 0)
                {
                    string[] arrTestResults = hiddenCourseID.Value.Trim().Split(new char[] { ';' });
                    foreach (string TestResults in arrTestResults)
                    {
                        if (TestResults.Trim().Length > 0)
                        {
                            string[] TestResult = TestResults.Split(new char[] { ':' });
                            string testType = TestResult[0];
                            string Result = TestResult[2];
                            string ApplyID = TestResult[1];
                            if (ApplyTestClass.hasTestResult(ApplyID, (string)Session["ApplyPeriod"], testType))
                                ApplyTestClass.UpdateTestResult(ApplyID, testType, Result, (string)Session["ApplyPeriod"]);
                            else
                                ApplyTestClass.InsertNewTestResult(ApplyID, testType, Result, (string)Session["ApplyPeriod"]);
                        }
                    }
                    hiddenCourseID.Value = string.Empty;
                    ApplyTestResule(0);
                }
            }
            catch (Exception ex)
            { 

            }
        }
    }
}
